package com.scoy.honeymei.fragment;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.activity.film.CinemaListActivity;
import com.scoy.honeymei.activity.film.FilmAdsActivity;
import com.scoy.honeymei.activity.film.FilmCinemaListActivity;
import com.scoy.honeymei.activity.home.CouponActivity;
import com.scoy.honeymei.activity.home.MemberTicketActivity;
import com.scoy.honeymei.activity.home.Travel0Activity;
import com.scoy.honeymei.activity.home.TravelDetailActivity;
import com.scoy.honeymei.activity.home.Trip0Activity;
import com.scoy.honeymei.activity.home.TripDetailActivity;
import com.scoy.honeymei.activity.mall.GoodsDetailActivity;
import com.scoy.honeymei.activity.mall.SearchHomeActivity;
import com.scoy.honeymei.adapter.FilmHomeNewAdapter;
import com.scoy.honeymei.adapter.GoodsHomeAdapter;
import com.scoy.honeymei.adapter.TravelAdapter;
import com.scoy.honeymei.adapter.TripAdapter;
import com.scoy.honeymei.base.BaseFragment;
import com.scoy.honeymei.bean.BannerBean;
import com.scoy.honeymei.bean.FilmBean;
import com.scoy.honeymei.bean.GengxinBean;
import com.scoy.honeymei.bean.GoodsBean;
import com.scoy.honeymei.bean.HomeBean;
import com.scoy.honeymei.bean.TravelBean;
import com.scoy.honeymei.bean.TripBean;
import com.scoy.honeymei.custem.PageMe;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.ApiCallBack;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.GlideImgUtil;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @ClassName: HomeFragment$
 * @Description: 首页首页
 * @Author: GaoZhan
 * @CreateDate: 2019/8/30/0030$ 上午 9:19$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/8/30/0030$ 上午 9:19$
 * @UpdateRemark: 更新内容
 */
public class HomeFragment extends BaseFragment {

    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.hf_search_tv)
    TextView hfSearchTv;
    @BindView(R.id.hf_banner)
    Banner hfBanner;
    @BindView(R.id.hf_f40_tv)
    TextView hfF40Tv;
    @BindView(R.id.hf_f41_tv)
    TextView hfF41Tv;
    @BindView(R.id.hf_f42_tv)
    TextView hfF42Tv;
    @BindView(R.id.hf_f43_tv)
    TextView hfF43Tv;

    @BindView(R.id.hf_f40_llt)
    View hfF40Llt;
    @BindView(R.id.hf_f41_llt)
    View hfF41Llt;
    @BindView(R.id.hf_f42_llt)
    View hfF42Llt;
    @BindView(R.id.hf_f43_llt)
    View hfF43Llt;

    @BindView(R.id.hf_f40_iv)
    ImageView hfF40Iv;
    @BindView(R.id.hf_f41_iv)
    ImageView hfF41Iv;
    @BindView(R.id.hf_f42_iv)
    ImageView hfF42Iv;
    @BindView(R.id.hf_f43_iv)
    ImageView hfF43Iv;

    @BindView(R.id.hf_f20_iv)
    ImageView hfF20Iv;
    @BindView(R.id.hf_f21_iv)
    ImageView hfF21Iv;
    @BindView(R.id.hf_filmmore_tv)
    TextView hfFilmmoreTv;
    @BindView(R.id.hf_film_rv)
    RecyclerView hfFilmRv;
    @BindView(R.id.hf_film_llt)
    LinearLayout hfFilmLlt;
    @BindView(R.id.hf_travelmore_tv)
    TextView hfTravelmoreTv;
    @BindView(R.id.hf_travel_rv)
    RecyclerView hfTravelRv;
    @BindView(R.id.hf_travel_llt)
    LinearLayout hfTravelLlt;
    @BindView(R.id.hf_tripmore_tv)
    TextView hfTripmoreTv;
    @BindView(R.id.hf_trip_rv)
    RecyclerView hfTripRv;
    @BindView(R.id.hf_trip_llt)
    LinearLayout hfTripLlt;
    @BindView(R.id.hf_goods_rv)
    RecyclerView hfGoodsRv;
    @BindView(R.id.hf_goods_llt)
    LinearLayout hfGoodsLlt;
    @BindView(R.id.nsv)
    NestedScrollView nsv;
    private Unbinder unbinder;
    private Context mContext;
    private ArrayList<FilmBean> datafilmlist;
    private FilmHomeNewAdapter filmAdapter;
    private ArrayList<TravelBean> datatravellist;
    private TravelAdapter travelAdapter;
    private TripAdapter tripAdapter;
    private ArrayList<TripBean> datatriplist;
    private ArrayList<GoodsBean> datagoodslist;
    private GoodsHomeAdapter mallAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, null, false);
        unbinder = ButterKnife.bind(this, view);
        mContext = getContext();

        initNormal();
        httpHome();

        return view;
    }

    @Override
    public void initNormal() {
        datafilmlist = new ArrayList<>();
        datatravellist = new ArrayList<>();
        datatriplist = new ArrayList<>();
        datagoodslist = new ArrayList<>();

        hfTripRv.setNestedScrollingEnabled(false);
        hfTravelRv.setNestedScrollingEnabled(false);
        hfFilmRv.setNestedScrollingEnabled(false);

        initFilmRV();
        initTravelRV();
        initTripRV();
        initGoodsRV();

        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);
    }

    @OnClick({R.id.hf_search_tv, R.id.hf_banner, R.id.hf_f40_llt, R.id.hf_f41_llt, R.id.hf_f42_llt, R.id.hf_f43_llt,
            R.id.hf_f20_iv, R.id.hf_f21_iv, R.id.hf_filmmore_tv, R.id.hf_travelmore_tv, R.id.hf_tripmore_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.hf_search_tv:
                Intent intent = new Intent(mContext, SearchHomeActivity.class);
                intent.putExtra("type", 0);
                startActivity(intent);
//                startActivity(new Intent(mContext, SearchHomeActivity.class));
                break;
            case R.id.hf_banner:
                break;
            //电影 more
            case R.id.hf_filmmore_tv:
                //电影票
            case R.id.hf_f40_llt:
                //0=未观看,1=已观看
                if (SPHelper.getInt(MeConstant.STATE_SHOW, 0) == 1) {
                    startActivity(new Intent(mContext, CinemaListActivity.class));
                } else {
                    startActivity(new Intent(mContext, FilmAdsActivity.class));
                }
                break;
            //商城
            case R.id.hf_f41_llt:
                if (onOneClick != null) {
                    onOneClick.oneClick(0);
                }
                break;
            //旅游 more
            case R.id.hf_travelmore_tv:
                //旅游
            case R.id.hf_f42_llt:
                startActivity(new Intent(mContext, Travel0Activity.class));
                break;
            //出行 more
            case R.id.hf_tripmore_tv:
                //乘车
            case R.id.hf_f43_llt:
                startActivity(new Intent(mContext, Trip0Activity.class));
                break;
            //优惠券
            case R.id.hf_f20_iv:
                startActivity(new Intent(mContext, CouponActivity.class));
                break;
            //影视会员专区
            case R.id.hf_f21_iv:
                startActivity(new Intent(mContext, MemberTicketActivity.class));
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    public interface OnOneClick {
        void oneClick(int pos);
    }

    public OnOneClick onOneClick;

    public void setOnOneClick(OnOneClick onOneClick) {
        this.onOneClick = onOneClick;
    }


    //获取首页数据
    private void httpHome() {
        HttpParams params = new HttpParams();

        HpGo.newInstance().HttpGo(getActivity(), MyUrl.HOME_HOME, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                Log.i("结果：",o);
                HomeBean homeBean = new Gson().fromJson(o, HomeBean.class);
                List<BannerBean> banner = homeBean.getBanner();
                ArrayList<String> bannerImg = new ArrayList<>();
                for (BannerBean bbean : banner) {
                    bannerImg.add(bbean.getImage());
                }

                PageMe.initBanner(getActivity(), hfBanner, banner);

                List<FilmBean> movie = homeBean.getMovie();
                datafilmlist.addAll(movie);
                filmAdapter.notifyDataSetChanged();

                List<TravelBean> tourn = homeBean.getTourn();
                datatravellist.addAll(tourn);
                travelAdapter.notifyDataSetChanged();

                List<TripBean> travel_list = homeBean.getTravel_list();
                if (travel_list == null || travel_list.size() == 0) {
                    hfF43Llt.setVisibility(View.GONE);
                    hfTripLlt.setVisibility(View.GONE);
                } else {
                    hfF43Llt.setVisibility(View.VISIBLE);
                    hfTripLlt.setVisibility(View.VISIBLE);
                }

                datatriplist.addAll(travel_list);
                tripAdapter.notifyDataSetChanged();


                List<GoodsBean> goods = homeBean.getGoods();
                datagoodslist.addAll(goods);
                mallAdapter.notifyDataSetChanged();

                GengxinBean gxObj = homeBean.getGengxin();
                String bdbb = MyUtil.getVerName(mContext).replace(".","");
                String khdbb = gxObj.getAndroid_v().replace(".","");
                Log.i("自身版本：",bdbb);
                Log.i("客户端版本：",khdbb);
                if(Integer.parseInt(bdbb) < Integer.parseInt(khdbb)){
                    version(gxObj.getIntro(),gxObj.getAndroid_url());
                }

                //icon
//                HomeBean.IconBean iconBean = homeBean.getIconBean();

//                GlideImgUtil.glidePic(mContext, iconBean.getIcon_img_one(), hfF40Iv);
//                hfF40Tv.setText(iconBean.getIcon_title_one());
//
//                GlideImgUtil.glidePic(mContext, iconBean.getIcon_img_two(), hfF41Iv);
//                hfF41Tv.setText(iconBean.getIcon_title_two());
//
//                GlideImgUtil.glidePic(mContext, iconBean.getIcon_img_three(), hfF42Iv);
//                hfF42Tv.setText(iconBean.getIcon_ititle_three());
//
//                GlideImgUtil.glidePic(mContext, iconBean.getIcon_img_four(), hfF43Iv);
//                hfF43Tv.setText(iconBean.getIcon_title_four());
//
//                GlideImgUtil.glidePic(mContext, iconBean.getCoupon_image(), hfF20Iv);
//                GlideImgUtil.glidePic(mContext, iconBean.getYingshi_image(), hfF21Iv);

            }
        });

    }

    public void version(String tishi,String url) {
        Dialog dialogNotice = new Dialog(mContext, R.style.BottomDialogStyle);
        View view = View.inflate(mContext, R.layout.dialog_tishi, null);
        TextView notTitleTv = view.findViewById(R.id.not_title_tv);
        TextView cancelBtn = view.findViewById(R.id.cancelBtn);
        TextView content = view.findViewById(R.id.content);
        TextView sureBtn = view.findViewById(R.id.sureBtn);
        notTitleTv.setText("版本更新提示");
        content.setText(tishi);
        sureBtn.setText("更新");
        cancelBtn.setVisibility(View.GONE);

        dialogNotice.setContentView(view);
        dialogNotice.setCanceledOnTouchOutside(true);
        Window dialogWindow = dialogNotice.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) getResources().getDimension(R.dimen.dp_300);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(lp);
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogNotice.dismiss();
            }
        });
        sureBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");

                Uri content_url = Uri.parse(url);
                intent.setData(content_url);
                startActivity(intent);
//                dialogNotice.dismiss();
            }
        });
        dialogNotice.show();
    }

    private void initFilmRV() {
        filmAdapter = new FilmHomeNewAdapter(mContext, datafilmlist, 1);
        hfFilmRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false));
        hfFilmRv.setAdapter(filmAdapter);
        filmAdapter.setOnOneClick(pos -> {
            FilmBean movieBean = datafilmlist.get(pos);
            Intent intentde = new Intent(mContext, FilmCinemaListActivity.class);
            intentde.putExtra("fId", movieBean.getMid());
            startActivity(intentde);
        });

    }

    private void initTravelRV() {
        travelAdapter = new TravelAdapter(mContext, datatravellist, 1);
        hfTravelRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
//        DividerItemDecoration did = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
//        did.setDrawable(getResources().getDrawable(R.drawable.divider_gray_line_pd));
//        hfTravelRv.addItemDecoration(did);
        hfTravelRv.setAdapter(travelAdapter);
        travelAdapter.setOnOneClick(pos -> {
            TravelBean travelBean = datatravellist.get(pos);
            Intent intent = new Intent(mContext, TravelDetailActivity.class);
            intent.putExtra("tid", travelBean.getId());
            startActivity(intent);
        });

    }

    private void initTripRV() {
        tripAdapter = new TripAdapter(mContext, datatriplist, 1);
        hfTripRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        hfTripRv.setAdapter(tripAdapter);
        tripAdapter.setOnOneClick(pos -> {
            TripBean tripBean = datatriplist.get(pos);
            Intent intentde = new Intent(mContext, TripDetailActivity.class);
            intentde.putExtra("tId", tripBean.getId());
            startActivity(intentde);
        });

    }

    private void initGoodsRV() {
        mallAdapter = new GoodsHomeAdapter(mContext, datagoodslist);
        GridLayoutManager glm = new GridLayoutManager(mContext, 2, RecyclerView.VERTICAL, false);
        hfGoodsRv.setLayoutManager(glm);
        hfGoodsRv.setAdapter(mallAdapter);
        mallAdapter.setOnOneClick(pos -> {
            GoodsBean goodsBean = datagoodslist.get(pos);
            int id = goodsBean.getId();
            Intent intentde = new Intent(mContext, GoodsDetailActivity.class);
            intentde.putExtra("goodsId", id + "");
            startActivityForResult(intentde, 0x871);
        });

    }


}
